package com.eastseeway.radioproject.mq;

import android.util.Log;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import org.greenrobot.eventbus.EventBus;

import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;


public class RabbitMQUtils {
    private Connection connection;
    private Channel channel;
    private ConnectionFactory factory;
    private String queueName;
    public RabbitMQUtils(String httpType, String userName, String password, String host, String port, String virtualHost, String exChangeName, String bindingKey,ResponseListener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (connection == null) {
                    // 创建一个连接
                    factory = new ConnectionFactory();
                    try {

                        // 连接配置
                        factory.setHost(host); // 服务器ip
                        factory.setPort(Integer.parseInt(port)); // 端口
                        factory.setUsername(userName); // 用户名
                        factory.setPassword(password); // 密码
                        factory.setVirtualHost(virtualHost);

                        // 创建一个新的代理连接
                        connection = factory.newConnection();
                        // 使用内部分配的通道号创建一个新通道
                        channel = connection.createChannel();
                        channel.exchangeDeclare(exChangeName, "direct", true); // 声明一个转发器
                        queueName = bindingKey;//channel.queueDeclare().getQueue();
                        channel.queueBind(queueName, exChangeName, bindingKey); // 绑定一个到转发器
                        Log.e("12121", "MQ: "+exChangeName+" "+queueName+" "+bindingKey);

                        startConsumer(listener);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    /**
     * 开始消费
     */
    private void startConsumer(ResponseListener listener) throws Exception {
        Log.e("startConsumer", "");

        // 定义回调接口DeliverCallback
            DeliverCallback deliverCallback  = (consumerTag, message) -> {
            String result = new String(message.getBody(), StandardCharsets.UTF_8);
            Log.i("12121 ", "DeliverCallback >>>"+result);
            listener.receive(result);
//            ComMessage comMessage=new ComMessage();
//            comMessage.code="1";// .setCode("1");
//            comMessage.message=result;
            // 创建一个事件
//            MessageEvent event = new MessageEvent();
//            event.setMessage(JSONUtil.toJsonStr(comMessage));

            // 通过EventBus发送事件
//            EventBus.getDefault().post(event);
        };

        // 启动基本消费，并传入回调接口c
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }

    /**
     * 关闭连接
     */
    public void close() throws Exception {
        channel.close();
        connection.close();
    }

    public interface ResponseListener {
        void receive(String message);
    }
}

